Employee Spotlight
From Academia to Industry
Philipp Burckhardt recounts his journey from childhood computer fascinations, to building an e-learning platform at Carnegie Mellon University, and on to his current role at Socket.
pkg-conf
Advanced tools
Package description
The pkg-conf npm package allows you to load configuration from the `package.json` file of your project. It helps in managing project-specific configurations in a centralized manner.
Load Configuration
This feature allows you to load configuration settings from the `package.json` file under a specific namespace (in this case, 'myApp'). The configuration is returned as a JavaScript object.
const pkgConf = require('pkg-conf');
(async () => {
const config = await pkgConf('myApp');
console.log(config);
})();
Default Values
You can provide default values for configuration settings. If the specified configuration is not found in the `package.json` file, the default values will be used.
const pkgConf = require('pkg-conf');
(async () => {
const config = await pkgConf('myApp', { default: 'value' });
console.log(config);
})();
Sync Loading
This feature allows you to load configuration settings synchronously. This can be useful in scenarios where asynchronous operations are not suitable.
const pkgConf = require('pkg-conf');
const config = pkgConf.sync('myApp');
console.log(config);
Cosmiconfig searches for and loads configuration files in various formats (JSON, YAML, JS, etc.). It is more flexible than pkg-conf as it supports multiple file formats and allows configuration files to be placed in different locations.
The rc package is used for loading configuration files in a variety of formats (JSON, INI, etc.) and from multiple sources (environment variables, command-line arguments, etc.). It provides more flexibility in terms of configuration sources compared to pkg-conf.
The config package provides a way to manage configuration files for different deployment environments. It supports hierarchical configurations and allows for environment-specific settings, which is more advanced compared to the simpler namespace-based approach of pkg-conf.
Readme
Get namespaced config from the closest package.json
Having tool specific config in package.json reduces the amount of metafiles in your repo (there are usually a lot!) and makes the config obvious compared to hidden dotfiles like .eslintrc
, which can end up causing confusion. XO, for example, uses the xo
namespace in package.json, and ESLint uses eslintConfig
. Many more tools supports this, like AVA, Babel, nyc, etc.
$ npm install pkg-conf
{
"name": "some-package",
"version": "1.0.0",
"unicorn": {
"rainbow": true
}
}
const pkgConf = require('pkg-conf');
(async () => {
const config = await pkgConf('unicorn');
console.log(config.rainbow);
//=> true
})();
It walks up parent directories until a package.json
can be found, reads it, and returns the user specified namespace or an empty object if not found.
Returns a Promise
for the config.
Returns the config.
Type: string
The package.json namespace you want.
Type: Object
Type: string
Default: process.cwd()
Directory to start looking up for a package.json file.
Type: Object
Default config.
Type: boolean
Default: false
Skip package.json
files that have the namespaced config explicitly set to false
.
Continues searching upwards until the next package.json
file is reached. This can be useful when you need to support the ability for users to have nested package.json
files, but only read from the root one, like in the case of electron-builder
where you have one package.json
file for the app and one top-level for development.
Example usage for the user:
{
"name": "some-package",
"version": "1.0.0",
"unicorn": false
}
Pass in the config
returned from any of the above methods.
Returns the filepath to the package.json file or null
when not found.
MIT © Sindre Sorhus
FAQs
Get namespaced config from the closest package.json
The npm package pkg-conf receives a total of 2,106,002 weekly downloads. As such, pkg-conf popularity was classified as popular.
We found that pkg-conf demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Employee Spotlight
Philipp Burckhardt recounts his journey from childhood computer fascinations, to building an e-learning platform at Carnegie Mellon University, and on to his current role at Socket.
Security News
Git dependencies in open source packages can introduce significant risks, including lack of version control, stability issues, dependency drift, and difficulty in auditing, making them potential targets for supply chain attacks.
Security News
Node.js has added experimental support for TypeScript, a move that highlights the growing importance of TypeScript in modern development.